package com.zelin.controller;

import com.zelin.pojo.Classes;
import com.zelin.pojo.PageResult;
import com.zelin.pojo.Result;
import com.zelin.pojo.Student;
import com.zelin.service.ClassesService;
import com.zelin.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
   //1. 列表学生
    @GetMapping("findall")
    public List<Student> findAll(){
        List<Student> studentList = studentService.findAll();
        return studentList;
    }
    //2.分页查询
    @GetMapping("findByPage")
    public PageResult findByPage(int page, int pageSize){
        return studentService.findByPage(page,pageSize);
    }
    //3.条件查询
    @PostMapping("search")
    public PageResult search(int page, int pageSize, @RequestBody(required = false) Student student){
        return studentService.search(page,pageSize,student);
    }
    //4.删除学生
    @GetMapping("dele")
    public Result dele(Integer[] ids){
        try {
            System.out.println(ids);
            studentService.dele(ids);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
    //5.添加学生
    @PostMapping("add")
    public Result add(@RequestBody Student student){
        try {
            studentService.add(student);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    //6.修改学生
    @PostMapping("update")
    public Result update(@RequestBody Student student){
        try {
            studentService.update(student);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }
}
